<?php

namespace App\Admin\Controllers;

use App\Models\DownloadZone;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Http\Request;

class DownloadZoneController extends AdminController
{
    protected $model = DownloadZone::class;

    protected function title()
    {
        return '下载专区';
    }

    protected function grid()
    {
        $grid = new Grid(new $this->model);

        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter(); //去掉默认的id过滤器

            $filter->like('name', trans('unit.name'));
        });

        //$grid->disableExport();

        $grid->model()->orderByDesc('id');
        $grid->column('title', '下载标题');
//        $grid->column('type', trans('download_zone.type'))->using(trans('download_zone.type_array'));
        $grid->column('files', trans('download_zone.files'))->display(function () {
            $file_name = $this->file_name;
            $path_info = request()->getPathInfo();
            return "<a href='{$path_info}/{$this->id}/edit'>{$file_name}</a>";
        });
        $grid->column('status', trans('admin.status'))->using(trans('admin.status_array'));
        $grid->column('created_at', trans('unit.created_at'));

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
        $script = <<<EOT

var search = $('.btn-dropbox');
search.hide();
EOT;
        Admin::script($script);
        $grid->disableActions();
        return $grid;
    }


    protected function form()
    {
        $form = new Form(new $this->model);
        $form->text('title', '下载标题')->rules('required');
//        $form->select('type', trans('download_zone.type'))->options(trans('download_zone.type_array'))->rules('required');
//        $form->switch('status', trans('download_zone.status'))->default(1);

        $form->file('files', trans('download_zone.files'))->divider(['disk' => 'qiniu', 'path' => 'avatar']);
//        $form->file('files', trans('download_zone.files'))->options([
//            'maxFileCount' => 1,
//            'maxFileSize' => 1024 * 500,
//            'allowedFileExtensions' => ['doc', 'xlsx', 'pdf'],
//            'dropZoneEnabled' => true,
//            'autoReplace' => true,
//            'browseOnZoneClick' => true,
//            'showUpload' => false,
//            'msgInvalidFileExtension' => '文件"{name}"的扩展名无效. 仅支持 "{extensions}" 文件.',
//            'dropZoneTitle' => '将文件拖放到此处',
//            'dropZoneClickTitle' => '或单击以选择文件',
//            'msgFilesTooMany' => '选择上载的文件数（2）超过了允许的最大限制1。',
//            'uploadAsync' => true,
//            'maxFileCount' => 1,
//            'uploadUrl' => 'bookccs/uploadfile',
//            'istrue' => true,
//
//
//        ])->help('可上传doc、pdf、xlsx格式文件,小于500M');


//        $form->qiniuImages('files', '商品图')
//            ->sortable() // 让图片可以拖拽排序
//            ->extraData(['disk' => 'qiniu', 'path' => 'avatar']) // 假如你有多个七牛配置，可以通过指定此处的 disk 进行上传， path 为文件路径的前缀
//            ->value(['http://url.com/a.jpg', 'http://url.com/b.jpg']); // 默认显示的图片数组，必须为 url

        $form->switch('status', trans('exhibitor_user.status'))->rules('required');
        $form->saving(function (Form $form) {
            $request = Request()->all();
            if (isset($request['files'])) {
                $files = $request['files'];
                $originalName = $files->getClientOriginalName();
                $form->model()->file_name = $originalName;
            }


        });
        return $form;
    }

}
